package cn.com.jslh.boarpro.main.model;

import android.content.Context;

import com.blankj.utilcode.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.main.contract.AddDuanNaiContract;
import cn.com.jslh.boarpro.tables.DuanNaiTable;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * Created by Administrator on 2017/10/18.
 */

public class AddDuanNaiModelImpl implements AddDuanNaiContract.Model {

    private List<JSONObject> zuhushes;
    private Context context;
    public AddDuanNaiModelImpl(Context context){
        this.context = context;
    }

    @Override
    public void loadZhuShe() {
        new WebService.Builder(context, "exportZhuShe", "WSZpigBaseDataFacade")
                .addProperty("lineID", SPUtils.getInstance().getString(BaseApplication.FLINEID))
                .addProperty("orgID", SPUtils.getInstance().getString(BaseApplication.FORGID))
                .addProperty("bizType", "dnZhuSheByOrg")
                .showDialog(true)
                .success(new WebService.NetSuccessListener() {
                    @Override
                    public void Success(String s) throws JSONException {
                        JSONObject jsonObject = new JSONObject(s);
                        JSONArray jsonArray = jsonObject.getJSONArray("array");
                        zuhushes = JSONUtil.JsonArray2List(jsonArray);
                    }
                })
                .post();
    }

    @Override
    public List<JSONObject> getZhuShes() {
        return zuhushes;
    }

    @Override
    public void savDuanNai(DuanNaiTable duanNaiTable) {
        DataBaseUtil.saveObjectData(context,DuanNaiTable.class,duanNaiTable);
    }
}
